package app.demo.controller;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springleaf.core.mvc.controller.JsonApiController;

import app.demo.model.Customer;
import app.demo.service.CustomerService;

@Controller
@RequestMapping("/customer")
public class CustomerAction extends JsonApiController<Customer>{
	
	@Resource
	private CustomerService customerService;
	
	/**
	 * 注册
	 * @param user
	 * @return
	 * @throws Exception 
	 */
	@Override
	public ModelAndView save(Customer user) throws Exception{
		if(StringUtils.isNotBlank(user.getUsername()) && StringUtils.isNotBlank(user.getPassword())){
			if(customerService.getUserByUsername(user.getUsername()) == null){
				user.setPassword(DigestUtils.md5Hex(user.getPassword()));
				if(customerService.save(user) != null){
					return renderJson(true,"用户添加成功");
				}
			}
			return renderJson(false,"用户名已经存在");
		}
		return renderJson(false,"数据不能为空");
	}
}
